package com.dancun.controller;


import com.dancun.entity.Student;
import com.dancun.entity.TDocument;
import com.dancun.entity.Teacher;
import com.dancun.service.IStudentService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 * <p>
 * <p>
 * <p>
 * 66
 *
 * @author jobob
 * @since 2020-10-02
 */
@RestController

public class StudentController {

    @Autowired
    IStudentService studentService;

    @RequestMapping("/stulist")
    public ModelAndView list() {
        ModelAndView mav = new ModelAndView("/Main/StudentList.jsp");
        List<Student> stu = studentService.list();
        mav.addObject("list", stu);
        return mav;
    }

    @RequestMapping("/stulogin")
    public ModelAndView login(Student stu) {
        ModelAndView mav = new ModelAndView("/Main/index.jsp");
        Map<String, Object> map = new HashMap<>();
        map.put("stno", stu.getStno());
        map.put("spwd", stu.getSpwd());
        if (studentService.listByMap(map).size() != 0) {
            mav.addObject("stu", studentService.listByMap(map).get(0));
            return mav;
        }
        mav.setViewName("/StuLogin.html");
        return mav;
    }

    @RequestMapping("/stusigup")
    public ModelAndView sigup(Student stu) {
        ModelAndView mav = new ModelAndView("/Main/index.jsp");
        if (studentService.save(stu)) {
            mav.addObject("stu", stu);
            return mav;
        }

        mav.setViewName("/StuSigup.html");
        return mav;
    }

    @RequestMapping("/stupersonal")
    public ModelAndView personal(Student stu) {//瀛︾敓涓汉淇℃伅
        ModelAndView mav = new ModelAndView("/Main/Personal.jsp");
        stu = (Student) studentService.getById(stu.getStno());
        mav.addObject("stu", stu);
        return mav;
    }

    @RequestMapping("/tasknew")
    public ModelAndView tasknew(Student stu) {
        ModelAndView mav = new ModelAndView("/Main/TaskNew.jsp");
        stu = (Student) studentService.getById(stu.getStno());
        mav.addObject("stu", stu);
        return mav;
    }

    @RequestMapping("/stulistjson")
    public String listStu() throws IOException {
        List<Student> list = studentService.list();
        JSONArray jsonarray = new JSONArray();
        String[][] str = new String[list.size()][2];
//        response.setContentType("text/html;charset=utf-8");
//        PrintWriter out = response.getWriter();
        for (Student s : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("sid", s.getStno());
            jsonObject.put("sname", s.getSname());
            jsonarray.add(jsonObject);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("stu", jsonarray);

        return jsonObject.toString();
    }

    @RequestMapping("/updateStu")
    public ModelAndView update(Student stu) {
        ModelAndView mav = new ModelAndView("/Mstulist");
        if (studentService.updateById(stu)) {
            return mav;
        }
        mav.setViewName("/Mstulist");
        return mav;
    }


    @RequestMapping("/delStu")
    public String delete(@Param("id") String stno, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if (studentService.removeById(stno)) {
            out.print("<script>alert('删除成功');window.location.href='Mstulist'</script>");
        } else {
            out.print("<script>alert('删除失败');window.location.href='Mstulist'</script>");
        }
        return null;

    }


}


